package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.configure.Result;
import com.store.entity.Material;
import com.store.entity.MaterialLog;
import com.store.mapper.MaterialLogMapper;
import com.store.mapper.MaterialMapper;
import com.store.service.MaterialLogService;
import com.store.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-04
 */
@Service
public class MaterialLogServiceImpl extends ServiceImpl<MaterialLogMapper, MaterialLog> implements MaterialLogService {

    @Resource
    MaterialLogMapper materialLogMapper;

    @Resource
    MaterialMapper materialMapper;

    /**
     * 对库存材料进行变动--变动日志
     * @param materialLog
     * @return
     */
    @Override
    public Result<MaterialLog> updateMaterialLog(MaterialLog materialLog) {
        Result<MaterialLog> result = new Result<>();
        try {
            if (StringUtils.isEmpty(materialLog.getMaterialLogId())) {
                //修改库存
                Material material = new Material();
                //材料ID与材料日志中的材料ID匹配
                material.setMaterialId(materialLog.getMaterialId());
                /**
                 * 如果变动类型为入库，那么是正数，增加， 否则为负数，都是减少。
                 * 三元运算符，如果是入库，那么加上变动数字，如果不是，都是减去那个数字，
                 * 给材料设置新总数
                 */
                Double amount = "1".equals(materialLog.getLogType())? materialLog.getAmount() : 0 - materialLog.getAmount();
                material.setAmount(amount);
                /**
                 * 计算完数字之后，修改材料表
                 */
                materialMapper.updateMaterial(material);
                //随机生成ID
                materialLog.setMaterialLogId(IDGenerator.getUUID("MLOG"));
                //创建时间为当前系统
                materialLog.setCreateTime(new Date());
                materialLogMapper.insert(materialLog);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MaterialLog> delMaterialLog(MaterialLog materialLog) {
        Result<MaterialLog> result = new Result<>();
        try {
            if (StringUtils.isEmpty(materialLog.getMaterialLogId())) {
                result.setMessage("materialLogId can't be empty");
                return result;
            }
            materialLogMapper.deleteById(materialLog.getMaterialLogId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MaterialLog> batchDelMaterialLog(MaterialLog materialLog) {
        Result<MaterialLog> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(materialLog.getIds())) {
                String[] ids = materialLog.getIds().split(",");
                for (String id : ids) {
                    materialLogMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MaterialLog> queryMaterialLog(MaterialLog materialLog) {
        Result<MaterialLog> result = new Result<>();
        try {
            QueryWrapper<MaterialLog> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if(!StringUtils.isEmpty(materialLog.getLogType())){
                wrapper.lambda().eq(MaterialLog::getLogType,materialLog.getLogType());
            }
            if(materialLog.getStartTime() != null){
                wrapper.lambda().ge(MaterialLog::getCreateTime,materialLog.getStartTime());
            }
            if(materialLog.getEndTime() != null){
                wrapper.lambda().le(MaterialLog::getCreateTime,materialLog.getEndTime());
            }
            List<MaterialLog> materialLogs = materialLogMapper.selectList(wrapper);
            result.setReturnList(materialLogs);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 库存记录--按类型查找（变动--调用废弃。。。）--materialLog.vue
     * @param materialLog
     * @return
     */
    @Override
    public Result<MaterialLog> queryMaterialLogByPage(MaterialLog materialLog) {
        Result<MaterialLog> result = new Result<>();

        try {
            if (materialLog.getCurrent() == null || materialLog.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<MaterialLog> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(materialLog.getLogType())){
                wrapper.lambda().eq(MaterialLog::getLogType,materialLog.getLogType());
            }
//            if(materialLog.getStartTime() != null){
//                wrapper.lambda().ge(MaterialLog::getCreateTime,materialLog.getStartTime());
//            }
//            if(materialLog.getEndTime() != null){
//                wrapper.lambda().le(MaterialLog::getCreateTime,materialLog.getEndTime());
//            }

            wrapper.orderByDesc("create_time");//倒叙
            //查询条件拼接，仅限单表查询可用
            IPage<MaterialLog> materialLogs = materialLogMapper.selectPage(new Page<>(materialLog.getCurrent(), materialLog.getSize()), wrapper);
            result.setReturnPage(materialLogs);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MaterialLog> queryMaterialLogById(MaterialLog materialLog) {
        Result<MaterialLog> result = new Result<>();
        try {
            MaterialLog returnMaterialLog = materialLogMapper.selectById(materialLog.getMaterialLogId());
            result.setReturnObj(returnMaterialLog);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
